package fr.pud.client.view;

import java.awt.Color;
import java.awt.Component;
import java.util.Collection;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.model.PUDStation;
import fr.pud.client.model.Query;
import fr.pud.client.model.Route;
import fr.pud.client.model.Vehicle;

/**
 * Apr 8, 2012 - Projet_Ose.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         SummarizeInfo.java
 *         This class create a ListCellRenderer to make easier the reading of
 *         the fleet and queries information.
 */
class RendererSummarizeInfo implements ListCellRenderer {
    protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        JLabel renderer = (JLabel) this.defaultRenderer
                .getListCellRendererComponent(list, value, index, isSelected,
                        cellHasFocus);
        renderer.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        index++;
        if (value instanceof Vehicle) {
            renderer.setText("<html><h2 style=\"color:red\">"
                    + LanguagesConstants.LANGUAGE
                            .getString(LanguagesConstants.VEHICLE) + " : "
                    + index + "</h2>" + ((Vehicle) value).toHTML() + "</html>");
        }
        else if (value instanceof Query) {
            renderer.setText("<html><h2 style=\"color:red\">"
                    + LanguagesConstants.LANGUAGE
                            .getString(LanguagesConstants.QUERY) + " : "
                    + index + "</h2>" + ((Query) value).toHTML() + " </html>");
        }
        else if (value instanceof Route) {
            renderer.setText("<html><h2 style=\"color:red\">"
                    + LanguagesConstants.LANGUAGE
                            .getString(LanguagesConstants.ROUTES) + " : "
                    + index + "</h2>" + ((Route) value).toHTML() + "</html>");
        }
        else if (value instanceof PUDStation) {
            renderer.setText("<html><h2 style=\"color:red\">"
                    + LanguagesConstants.LANGUAGE
                            .getString(LanguagesConstants.STATION_ADDRESS)
                    + " : " + index + "</h2>" + ((PUDStation) value).toHTML()
                    + "</html>");
        }
        return renderer;
    }
}

/**
 * Apr 3, 2012 - OSE
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet.
 *         Ecole des Mines de Nantes SummarizeQueries.java
 * @param <T>
 */
public class SummarizeInfo<T> extends JList implements Observer {
    /**
     * The serial to serialize the info
     */
    private static final long serialVersionUID = 4674444582949271109L;
    /**
     * The collection to observe
     */
    private Collection<T>     collection;

    /**
     * 
     */
    /**
     * The default constructor which add this to the observer's list of o
     * @param theCollection
     *            the collection to display
     * @param o
     *            the Observable to watch
     */
    public SummarizeInfo(Collection<T> theCollection, Observable o) {
        o.addObserver(this);
        this.collection = theCollection;
        RendererSummarizeInfo renderer = new RendererSummarizeInfo();
        this.setListData(this.collection.toArray());
        this.setCellRenderer(renderer);
    }

    /**
     * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
     */
    @Override
    public void update(Observable aO, Object aArg) {
        this.setListData(this.collection.toArray());
    }
}
